package config;

import core.util.JacksonUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScan("com.controller")
public class MvcConfig implements WebMvcConfigurer {
    /**
     * 这种全局配置利用的是mvc框架内的拦截器实现，如果项目中编写了自己的拦截器，
     * 会导致自己编写的拦截器在跨域处理拦截器之前运行，并且没法调整顺序，
     * 会导致自己编写的拦截器要处理跨域问题，特别是要处理复杂跨域请求的预请求（OPTIONS),
     * 所以，这里不用这种方式，而改用CorsFilter的方式，见SystemConfig里的过滤器设置
     *
     * @param registry
     */
/*    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "HEAD")
                .allowedOrigins("http://127.0.0.1:5500");
    }*/
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter("yyyy-MM-dd"));
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(JacksonUtils.getObjectMapper());
        converters.add(0, converter);
    }


}
